package com.fira.edu.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.bean.DozerUtils;
import com.fira.common.utils.uuid.IdUtils;
import com.fira.edu.domain.EduCourseSchedule;
import com.fira.edu.domain.EduCourseScheduleTime;
import com.fira.edu.domain.dto.ClassDTO;
import com.fira.edu.domain.dto.CourseRollCallQueryDTO;
import com.fira.edu.domain.dto.QueryClassRollCallDTO;
import com.fira.edu.domain.vo.*;
import com.fira.edu.mapper.EduClassRollCallMapper;
import com.fira.edu.mapper.EduCourseScheduleMapper;
import com.fira.edu.mapper.EduCourseScheduleTimeMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fira.edu.mapper.EduClassMapper;
import com.fira.edu.domain.EduClass;
import com.fira.edu.service.IEduClassService;
import com.fira.common.core.text.Convert;

/**
 * 班级信息Service业务层处理
 * 
 * @author zhushuyong
 * @date 2021-10-12
 */
@Service
public class EduClassServiceImpl implements IEduClassService 
{
    @Autowired
    private EduClassMapper eduClassMapper;
    @Autowired
    private EduCourseScheduleMapper eduCourseScheduleMapper;
    @Autowired
    private EduCourseScheduleTimeMapper eduCourseScheduleTimeMapper;
    @Autowired
    private EduClassRollCallMapper eduClassRollCallMapper;

    /**
     * 查询班级信息
     * 
     * @param id 班级信息ID
     * @return 班级信息
     */
    @Override
    public EduClass selectEduClassById(String id)
    {
        return eduClassMapper.selectEduClassById(id);
    }

    /**
     * 查询班级信息列表
     * 
     * @param eduClass 班级信息
     * @return 班级信息
     */
    @Override
    public List<EduClass> selectEduClassList(EduClass eduClass)
    {
        return eduClassMapper.selectEduClassList(eduClass);
    }

    /**
     * 新增班级信息
     * 
     * @param classDTO 班级信息
     * @return 结果
     */
    @Override
    public AddClassVO insertEduClass(ClassDTO classDTO)
    {
        classDTO.setId(IdUtils.fastSimpleUUID());
        EduClass eduClass = DozerUtils.copyProperties(classDTO,EduClass.class);
        eduClass.setCreateTime(DateUtils.getNowDate());
        eduClassMapper.insertEduClass(eduClass);
        AddClassVO vo = new AddClassVO();
        vo.setId(eduClass.getId());
        vo.setCourseId(eduClass.getCourseId());
        return vo;
    }

    /**
     * 修改班级信息
     * 
     * @param eduClass 班级信息
     * @return 结果
     */
    @Override
    public int updateEduClass(EduClass eduClass)
    {
        eduClass.setUpdateTime(DateUtils.getNowDate());
        return eduClassMapper.updateEduClass(eduClass);
    }

    /**
     * 删除班级信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEduClassByIds(String ids)
    {
        return eduClassMapper.deleteEduClassByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除班级信息信息
     * 
     * @param id 班级信息ID
     * @return 结果
     */
    @Override
    public int deleteEduClassById(String id)
    {
        return eduClassMapper.deleteEduClassById(id);
    }

    /**
     * 小程序-查询租户下的班级列表
     * @param tenantId
     * @return
     */
    @Override
    public List<ClassVO> selectClassInfoList(String tenantId){
        List<ClassVO> voList = eduClassMapper.selectClassInfoList(tenantId);
        if(CollectionUtils.isNotEmpty(voList)){
            //查询班级排课信息
            voList.forEach(item -> {
                EduCourseSchedule eduCourseSchedule = new EduCourseSchedule();
                eduCourseSchedule.setCourseId(item.getCourseId());
                eduCourseSchedule.setClassId(item.getClassId());
                List<EduCourseSchedule> courseScheduleList = eduCourseScheduleMapper.selectEduCourseScheduleList(eduCourseSchedule);
                if(CollectionUtils.isNotEmpty(courseScheduleList)){
                    List<ClassScheduleVO> scheduleVOList = new ArrayList<>();
                    courseScheduleList.forEach(schedule -> {
                        //查询课程时间安排明细
                        EduCourseScheduleTime time = new EduCourseScheduleTime();
                        time.setCourseScheduleId(schedule.getId());
                        List<EduCourseScheduleTime> timeList = eduCourseScheduleTimeMapper.selectEduCourseScheduleTimeList(time);
                        timeList.forEach(scheduleTime -> {
                            ClassScheduleVO classScheduleVO = new ClassScheduleVO();
                            classScheduleVO.setCourseWeek("每"+scheduleTime.getCourseWeek());
                            classScheduleVO.setCourseStartTime(scheduleTime.getCourseStartTime());
                            classScheduleVO.setCourseEndTime(scheduleTime.getCourseEndTime());
                            scheduleVOList.add(classScheduleVO);
                        });
                    });
                    item.setClassScheduleVOList(scheduleVOList);
                }
            });
        }
        return voList;
    }

    /**
     * 小程序-查询班级上课点名信息
     *
     * @param queryClassRollCallDTO
     * @return
     */
    @Override
    public ClassRollCallVO selectClassRollCall(QueryClassRollCallDTO queryClassRollCallDTO) {
        ClassRollCallVO classRollCallVO = eduClassMapper.selectClassRollCall(queryClassRollCallDTO);
        //查询点名学生列表
        if(classRollCallVO != null){
            CourseRollCallQueryDTO dto = new CourseRollCallQueryDTO();
            dto.setClassId(queryClassRollCallDTO.getClassId());
            dto.setCourseId(classRollCallVO.getCourseId());
            List<ClassStudentRollCallVO> classStudentRollCallVOList = eduClassRollCallMapper.selectClassStudentRollCall(dto);
            classRollCallVO.setClassStudentRollCallVOList(classStudentRollCallVOList);
        }
        return classRollCallVO;
    }

    /**
     * 统计当日新开班级数量
     *
     * @return
     */
    @Override
    public Integer selectCountClassOfToday() {
        return eduClassMapper.selectCountClassOfToday();
    }
}
